﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.EFDBFirst;

namespace YiYiTong.Rocket.Api.Biz
{
    public class AccountService : ApiService
    {
        #region 账户相关
        /// <summary>
        /// 操作提现银行
        /// </summary>
        public void EditCashBank(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            int bank_id = dic.GetValue<int>("bank_id", 0);
            string bank_account = dic.GetValue<string>("bank_account");            //银行账号
            string branch_bank_name = dic.GetValue<string>("branch_bank_name");    //支行名称
            string bank_account_name = dic.GetValue<string>("bank_account_name");  //开户名
            string code = dic.GetValue<string>("code");         //验证码

            var result = AccountBiz.Instance.EditCashBank(apiContext.CurrUserId, bank_id, bank_account, branch_bank_name, bank_account_name, code);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(new { cash_bank_id = result.Item3 });
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取用户提现银行
        /// </summary>
        /// <param name="apiContext"></param>
        public void GetCashBank(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            var result = AccountBiz.Instance.GetCashBank(apiContext.CurrUserId);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(result.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "用户提现银行为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 我的销售业绩
        /// </summary>
        public void GetSaleList(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string beg_saledate = dic.GetValue<string>("beg_saledate");
            string end_saledate = dic.GetValue<string>("end_saledate");
            int pageindex = dic.GetValue<int>("pageindex", 1);
            int pagerows = dic.GetValue<int>("pagerows", 10);

            if (string.IsNullOrEmpty(beg_saledate) || string.IsNullOrEmpty(end_saledate))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "时间间隔不能为空";
                apiContext.apiHttpResp = apiResponse;
                return;
            }
            if (!Utils.IsDate(beg_saledate) || !Utils.IsDate(end_saledate))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "日期格式错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            if (Utils.CompareDate(beg_saledate, end_saledate, 1))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "时间间隔不能超过1个月";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            Tuple<bool, SaleSettlementListModel> tupleResult = OrderBiz.Instance.GetSaleSettlementList(apiContext.CurrUserId, beg_saledate, end_saledate, pageindex, pagerows);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.SerializeDate(tupleResult.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "我的销售业绩为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取用户邀请码
        /// </summary>
        /// <param name="apiContext"></param>
        public void GetInviteCode(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            int is_used = dic.GetValue<int>("is_used", 0);
            int pageindex = dic.GetValue<int>("pageindex", 1);
            int pagerows = dic.GetValue<int>("pagerows", 10);

            Tuple<bool, InviteCodeModel> tupleResult = AccountBiz.Instance.GetInviteCode(apiContext.CurrUserId, is_used, pageindex, pagerows);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(tupleResult.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "邀请码为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取用户推荐人
        /// </summary>
        /// <param name="apiContext"></param>
        public void GetUserInvite(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;


            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var inviteUser = dbcontext.ADZ_InviteCode.FirstOrDefault(t => t.rel_uid == apiContext.CurrUserId);
            if (inviteUser != null)
            {
                var userinfo = dbcontext.ADZ_MemberMaster.FirstOrDefault(t => t.uid == inviteUser.uid);
                if (userinfo != null)
                {
                    apiResponse.retCode = ResultEnum.Success;
                    apiResponse.data = JsonHelper.Serialize(new
                    {
                        userinfo.account_mobile,
                        userinfo.member_name,
                        userinfo.uid
                    });
                    apiContext.apiHttpResp = apiResponse;
                    return;
                }
            }


            apiResponse.retCode = ResultEnum.NoData;
            apiResponse.message = "没有推荐人";

            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 填写邀请码
        /// </summary>
        public void WriteInviteCode(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string code = dic.GetValue<string>("code");
            if (string.IsNullOrEmpty(code))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "请输入邀请码";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var result = AccountBiz.Instance.WriteInviteCode(apiContext.CurrUserId, code);
            if (result.Item1)
                apiResponse.retCode = ResultEnum.Success;
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 绑定邀请码
        /// </summary>
        public void BindInviteCode(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string code = dic.GetValue<string>("code");             //邀请码
            string mobile = dic.GetValue<string>("mobile");         //手机号
            string validcode = dic.GetValue<string>("validcode");   //手机验证码
            if (string.IsNullOrEmpty(mobile) || string.IsNullOrEmpty(validcode) || string.IsNullOrEmpty(code))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //检查手机号是否合法
            if (!Utils.CheckMobile(mobile))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "手机号码错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //检查验证码是否合法
            if (!Utils.CheckMobileCode(validcode))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "验证码6位数字";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var result = AccountBiz.Instance.BindInviteCode(mobile, validcode, code);
            if (result.Item1)
                apiResponse.retCode = ResultEnum.Success;
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 会员升级申请
        /// </summary>
        public void ApplyMemberUp(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string contact_name = dic.GetValue<string>("contact_name");
            string telephone = dic.GetValue<string>("telephone");
            if (string.IsNullOrEmpty(telephone) || string.IsNullOrEmpty(contact_name))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //检查手机号是否合法
            if (!Utils.CheckMobile(telephone))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "手机号码错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var result = AccountBiz.Instance.ApplyMemberUp(apiContext.CurrUserId, contact_name, telephone);
            if (result.Item1)
                apiResponse.retCode = ResultEnum.Success;
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取会员升级申请
        /// </summary>
        /// <param name="apiContext"></param>
        public void GetApplyUp(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();

            var result = AccountBiz.Instance.GetMemberApplyUp(apiContext.CurrUserId);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(result.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "会员升级申请信息为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 我是卖家
        /// </summary>
        public void ApplySeller(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string company_name = dic.GetValue<string>("company_name");         //公司名称
            string contact_name = dic.GetValue<string>("contact_name");         //联系人
            string telephone = dic.GetValue<string>("telephone");               //手机号
            string sex = dic.GetValue<string>("sex");                           //性别
            string email = dic.GetValue<string>("email");
            string qq = dic.GetValue<string>("qq");
            string company_address = dic.GetValue<string>("company_address");   //公司地址
            if (string.IsNullOrEmpty(company_name) || string.IsNullOrEmpty(telephone) || string.IsNullOrEmpty(contact_name))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //检查手机号是否合法
            if (!Utils.CheckMobile(telephone))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "手机号码错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            if (!string.IsNullOrEmpty(qq))
            {
                if (!Utils.CheckNumber(qq))
                {
                    apiResponse.retCode = ResultEnum.ParamError;
                    apiResponse.message = "qq号码错误";
                    apiContext.apiHttpResp = apiResponse;
                    return;
                }
            }

            var result = AccountBiz.Instance.ApplySeller(apiContext.CurrUserId, company_name, contact_name, telephone, sex, email, qq, company_address);
            if (result.Item1)
                apiResponse.retCode = ResultEnum.Success;
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取卖家信息
        /// </summary>
        public void GetSellerInfo(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();

            var result = AccountBiz.Instance.GetSellerInfo(apiContext.CurrUserId);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(result.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "卖家申请信息为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 完善用户信息
        /// </summary>
        public void WriteDetails(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string company_name = dic.GetValue<string>("company_name");         //公司名称
            string company_address = dic.GetValue<string>("company_address");   //公司地址  
            string company_telephone = dic.GetValue<string>("company_telephone");//公司电话 
            string id_number = dic.GetValue<string>("id_number");               //身份证号
            string contact_mobile = dic.GetValue<string>("contact_mobile");    //联系手机号 
            string real_name = dic.GetValue<string>("real_name");               //姓名
            int customer_type = dic.GetValue<int>("customer_type"); //客户类型

            //检查手机号是否合法
            if (!string.IsNullOrWhiteSpace(contact_mobile))
            {
                if (!Utils.CheckMobile(contact_mobile))
                {
                    apiResponse.retCode = ResultEnum.ParamError;
                    apiResponse.message = "手机号码错误";
                    apiContext.apiHttpResp = apiResponse;
                    return;
                }
            }

            //检查身份证号是否合法
            if (!string.IsNullOrWhiteSpace(id_number))
            {
                if (!Utils.CheckIDNumber(id_number))
                {
                    apiResponse.retCode = ResultEnum.ParamError;
                    apiResponse.message = "身份证号错误";
                    apiContext.apiHttpResp = apiResponse;
                    return;
                }
            }

            Tuple<bool, string> tupleResult = AccountBiz.Instance.WriteDetails(apiContext.CurrUserId, real_name, id_number, contact_mobile, company_name, company_address, company_telephone, customer_type);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = tupleResult.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取账户个人及公司信息
        /// </summary>
        public void GetDetails(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            Tuple<bool, MemberDetailsModel> tupleResult = AccountBiz.Instance.GetDetails(apiContext.CurrUserId);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(tupleResult.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "基本信息为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 账户基本信息
        /// </summary>
        public void GetInfo(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            Tuple<bool, MemberModel> tupleResult = AccountBiz.Instance.GetInfo(apiContext.CurrUserId);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(tupleResult.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "会员信息为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 设置用户昵称
        /// </summary>
        public void EditNick(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string nickname = dic.GetValue<string>("nickname");

            if (string.IsNullOrWhiteSpace(nickname))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "昵称不能为空";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            Tuple<bool, string> tupleResult = AccountBiz.Instance.EditNick(apiContext.CurrUserId, nickname);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = tupleResult.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 设置头像
        /// </summary>
        public void EditHeader(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string headerdata = dic.GetValue<string>("headerdata"); //base64头像流数据
            string filetype = dic.GetValue<string>("filetype");     //图片格式 如jpg,png

            if (string.IsNullOrWhiteSpace(headerdata) || string.IsNullOrWhiteSpace(filetype))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var fileTypeLists = new string[] { "png", "jpg" };
            if (!fileTypeLists.Any(b => b.Contains(filetype)))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "头像格式错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            Tuple<bool, string> tupleResult = AccountBiz.Instance.EditHeader(apiContext.CurrUserId, headerdata, filetype);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = tupleResult.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取用户会员等级
        /// </summary>
        /// <param name="apiContext"></param>
        public void GetMemberLevel(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            Tuple<bool, MemberModel> tupleResult = AccountBiz.Instance.GetInfo(apiContext.CurrUserId);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(new { member_level = tupleResult.Item2 });
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "会员等级信息为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 我的账单
        /// </summary>
        /// <param name="apiContext"></param>
        public void GetBill(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            int ordertype = dic.GetValue<int>("ordertype", -10);
            int pageindex = dic.GetValue<int>("pageindex", 1);
            int pagerows = dic.GetValue<int>("pagerows", 10);

            Tuple<bool, BillModel> tupleResult = AccountBiz.Instance.GetBill(apiContext.CurrUserId, ordertype, pageindex, pagerows);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(tupleResult.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "我的账单为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 支付密码设置
        /// </summary>
        /// <returns></returns>
        public void SetPayPwd(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string paypwd = dic.GetValue<string>("paypwd");
            string confirmpaypwd = dic.GetValue<string>("confirmpaypwd");
            string code = dic.GetValue<string>("code");         //验证码

            if (string.IsNullOrEmpty(paypwd) || string.IsNullOrEmpty(confirmpaypwd) || string.IsNullOrEmpty(code))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //检查验证码是否合法
            if (!Utils.CheckMobileCode(code))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "验证码6位数字";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            if (!paypwd.Equals(confirmpaypwd))
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = "支付密码不一致";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var tupleResult = AccountBiz.Instance.SetPayPwd(apiContext.CurrUserId, paypwd, code);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = tupleResult.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 填写发票
        /// </summary>
        public void WriteInvoice(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            long id = dic.GetValue<long>("id", 0);
            int invoice_type = dic.GetValue<int>("invoicetype", 0);
            string invoice_title = dic.GetValue<string>("invoice_title");
            string invoice_content = dic.GetValue<string>("invoice_content");
            string taxpayer_ID = dic.GetValue<string>("taxpayer_id");

            if (string.IsNullOrEmpty(invoice_title) || string.IsNullOrEmpty(invoice_content))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            if (invoice_type == (int)InvoiceType.Company)
            {
                if (string.IsNullOrEmpty(taxpayer_ID))
                {
                    apiResponse.retCode = ResultEnum.ParamError;
                    apiResponse.message = "纳税人识别号不为空";
                    apiContext.apiHttpResp = apiResponse;
                    return;
                }
            }

            if (!EnumHelper.IsDefined<InvoiceType>(invoice_type))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "发票类型错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var tupleResult = AccountBiz.Instance.WriteInvoice(id, apiContext.CurrUserId, invoice_type, invoice_title, invoice_content, taxpayer_ID);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(new { id = tupleResult.Item2 });
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = tupleResult.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取用户发票
        /// </summary>
        public void GetInvoice(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();

            var result = AccountBiz.Instance.GetInvoice(apiContext.CurrUserId);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(new { list = result.Item2 });
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = result.Item3;
            }
            apiContext.apiHttpResp = apiResponse;
        }
        #endregion

        #region 收货地址
        /// <summary>
        /// 新增收货地址
        /// </summary>
        public void AddAddress(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string cityid = dic.GetValue<string>("cityid");
            string cityname = dic.GetValue<string>("cityname");
            string address = dic.GetValue<string>("address");
            string receiver = dic.GetValue<string>("receiver");
            string telephone = dic.GetValue<string>("telephone");
            int isDefault = dic.GetValue<int>("isDefault", 0);
            if (string.IsNullOrEmpty(cityid) || string.IsNullOrEmpty(cityname) || string.IsNullOrEmpty(address)
                || string.IsNullOrEmpty(receiver) || string.IsNullOrEmpty(telephone))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //检查手机号是否合法
            if (!Utils.CheckMobile(telephone))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "手机号码错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //是否默认，0 否 1 是
            if (!EnumHelper.IsDefined<AddressDefault>(isDefault))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "默认收货地址错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            Tuple<bool, long, string> tupleResult = AccountBiz.Instance.AddAddress(apiContext.CurrUserId, cityid, cityname, address, receiver, telephone, isDefault);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(new { seq_id = tupleResult.Item2 });
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = tupleResult.Item3;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 编辑收货地址
        /// </summary>
        public void EditAddress(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            long id = dic.GetValue<long>("id", 0);
            string cityid = dic.GetValue<string>("cityid");
            string cityname = dic.GetValue<string>("cityname");
            string address = dic.GetValue<string>("address");
            string receiver = dic.GetValue<string>("receiver");
            string telephone = dic.GetValue<string>("telephone");
            int isDefault = dic.GetValue<int>("isDefault", 0);
            if (id <= 0 || string.IsNullOrEmpty(cityid) || string.IsNullOrEmpty(cityname) || string.IsNullOrEmpty(address)
                || string.IsNullOrEmpty(receiver) || string.IsNullOrEmpty(telephone))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //检查手机号是否合法
            if (!Utils.CheckMobile(telephone))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "手机号码错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //是否默认，0 否 1 是
            if (!EnumHelper.IsDefined<AddressDefault>(isDefault))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "默认收货地址错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            Tuple<bool, string> tupleResult = AccountBiz.Instance.EditAddress(id, apiContext.CurrUserId, cityid, cityname, address, receiver, telephone, isDefault);
            if (tupleResult.Item1)
                apiResponse.retCode = ResultEnum.Success;
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = tupleResult.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 删除收货地址
        /// </summary>
        public void DelAddress(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            long id = dic.GetValue<long>("id", 0);
            if (id <= 0)
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var result = AccountBiz.Instance.DelAddress(id, apiContext.CurrUserId);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(new { seq_id = result.Item2 });
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item3;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取收货地址列表
        /// </summary>
        public void GetAddressList(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();

            Tuple<bool, List<AddressInfo>> result = AccountBiz.Instance.GetAddressList(apiContext.CurrUserId);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(new { list = result.Item2 });
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "收货地址为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取单个收货地址
        /// </summary>
        public void GetAddress(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            long id = dic.GetValue<long>("id", 0);
            if (id <= 0)
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            AddressInfo info = AccountBiz.Instance.GetAddress(id);
            if (info != null && info.seq_id != 0)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(info);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "收货地址为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }
        #endregion

        #region 账户充值
        /// <summary>
        /// 支付宝充值/微信充值
        /// </summary>
        public void Charge(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            decimal amt = dic.GetValue<decimal>("amt", 0);         //充值金额
            int channelType = dic.GetValue<int>("channelType", 0); //充值类型 1 支付宝 2 微信 3 银联
            if (amt <= 0 || amt > 50000)
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "充值金额错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            if (!EnumHelper.IsDefined<ChannelType>(channelType))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "充值类型错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            Tuple<bool, string, string> result = AccountBiz.Instance.Charge(apiContext.CurrUserId, amt, channelType);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = result.Item2;
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item3;
            }
            apiContext.apiHttpResp = apiResponse;
        }
        #endregion

        #region 我的关注
        /// <summary>
        /// 新增or取消关注
        /// </summary>
        public void HandleFollow(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            int follow_type = dic.GetValue<int>("follow_type", 0);  //关注类型 1 商品分类 2 商品品牌
            long follow_id = dic.GetValue<long>("follow_id", 0);    //对应id
            int op_type = dic.GetValue<int>("op_type", 0);          //1 新增关注 2 取消关注

            if (follow_type <= 0 || follow_id <= 0)
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }
            if (!EnumHelper.IsDefined<FollowType>(follow_type))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "关注类型错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            if (!EnumHelper.IsDefined<OpType>(op_type))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "操作错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var result = AccountBiz.Instance.HandleFollow(apiContext.CurrUserId, op_type, follow_type, follow_id);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;

        }

        /// <summary>
        /// 我的关注
        /// </summary>
        public void GetFollow(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            int follow_type = dic.GetValue<int>("follow_type", 0);  //关注类型 1 商品分类 2 商品品牌

            if (follow_type <= 0)
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }
            if (!EnumHelper.IsDefined<FollowType>(follow_type))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "关注类型错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }
            var result = AccountBiz.Instance.GetFollow(apiContext.CurrUserId, follow_type);
            if (result.list.Count > 0)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(result);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "我的关注为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }
        #endregion

        /// <summary>
        /// 编辑基本信息
        /// </summary>
        /// <param name="apiContext"></param>
        public void EditDetails(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string real_name = dic.GetValue<string>("real_name");           //姓名
            short sex = dic.GetValue<short>("sex", 0);                      //性别 1 男 2 女
            string company_name = dic.GetValue<string>("company_name");     //公司名
            string company_address = dic.GetValue<string>("address");               //地址
            string company_telephone = dic.GetValue<string>("contact_mobile"); //联系方式
            int customer_type = dic.GetValue<int>("customer_type"); //客户类型


            //检查手机号是否合法
            if (company_name.Length < 3)
            {

                    apiResponse.retCode = ResultEnum.ParamError;
                    apiResponse.message = "请填写正确的公司名称";
                    apiContext.apiHttpResp = apiResponse;
                    return;

            }

            var result = AccountBiz.Instance.SaveDetails(apiContext.CurrUserId, real_name, sex, company_name, "", "", "", company_telephone, company_address, customer_type);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }
    }
}